﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Vit.Common.Providers.Logging
{
    public abstract class WebLogProviderBase : ILogProvider
    {
        public WebLogProviderBase()
        {
            TraceContextEnabled = true;
        }

        protected virtual string CreateContextInfo()
        {
            StringBuilder sb = new StringBuilder();
            HttpRequest request = HttpContext.Current.Request;
            sb.AppendFormat("RawUrl:{0};", request.RawUrl);
            sb.Append("QueryString:[");
            foreach(var qs in request.QueryString.AllKeys)
            {
                sb.AppendFormat("{0}={1};", qs, request.QueryString[qs]);
            }
            sb.Append("];Form:[");
            foreach (var f in request.Form.AllKeys)
            {
                sb.AppendFormat("{0}={1};", f, request.Form[f]);
            }
            return sb.ToString();
        }

        #region ILogProvider Members

        public abstract void Log(LogInfo logInfo);

        public abstract void Log(Exception exception);

        public abstract void Log(string source, string message);

        public abstract void Log(Exception exception, LogLevel logLevel);

        public abstract void Log(string source, string message, LogLevel logLevel);

        public abstract void RemoveAll(DateTime beforeDate);

        public abstract void Clear();

        #endregion

        public bool TraceContextEnabled { get; set; }

        #region IProvider Members

        public virtual string Name
        {
            get { return this.GetType().FullName; }
        }

        #endregion
    }
}
